package com.sxpi.user.controller;

import com.sxpi.common.result.R;
import com.sxpi.user.dto.UserAmountDto;
import com.sxpi.user.service.useramount.IUserAmountRecordRedisService;
import com.sxpi.user.service.useramount.IUserAmountRecordRedissonService;
import com.sxpi.user.service.useramount.IUserAmountRecordService;
import com.sxpi.user.service.useramount.IUserAmountRecordZookeeperService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: 董伟豪
 * @Date: 2022/11/8 21:13
 */
@RestController
@Slf4j
public class UserAmountRecordController {

    @Autowired
    private IUserAmountRecordService userAmountRecordService;

    @Autowired
    private IUserAmountRecordRedisService userAmountRecordRedisService;

    @Autowired
    private IUserAmountRecordZookeeperService userAmountRecordZookeeperService;

    @Autowired
    private IUserAmountRecordRedissonService userAmountRecordRedissonService;

    /**
     * 用户提现
     * @return
     * @paramp loginVo
     */
    @PostMapping("/user/takemoney")
    public R takeMoney(@RequestBody UserAmountDto userAmountDto) {
        return userAmountRecordService.takeMoney(userAmountDto);
    }

    /**
     * 用户提现
     * 数据库-分布式锁解决方案 - 悲观锁
     * @return
     * @paramp loginVo
     */
    @PostMapping("/user/takemoneypessimism")
    public R takeMoneyPessimismLock(@RequestBody UserAmountDto userAmountDto) {
        return userAmountRecordService.takeMoneyPessimismLock(userAmountDto);
    }

    /**
     * 用户提现
     * 数据库-分布式锁解决方案 - 乐观锁
     * @return
     * @paramp loginVo
     */
    @PostMapping("/user/takemoneyoptimism")
    public R takeMoneyOptimismLock(@RequestBody UserAmountDto userAmountDto) {
        return userAmountRecordService.takeMoneyOptimismLock(userAmountDto);
    }

    /**
     * 用户提现
     * 数据库-分布式锁解决方案 - redis不可重入
     * @return
     * @paramp loginVo
     */
    @PostMapping("/user/takemoneyredis")
    public R takeMoneyRedisLock(@RequestBody UserAmountDto userAmountDto) {
        return userAmountRecordRedisService.takeMoneyRedisLock(userAmountDto);
    }

    /**
     * 用户提现
     * 数据库-分布式锁解决方案 - zookeeper可重入
     * @return
     * @paramp loginVo
     */
    @PostMapping("/user/takemoneyzookeeper")
    public R takeMoneyZookeeperLock(@RequestBody UserAmountDto userAmountDto) {
        return userAmountRecordZookeeperService.takeMoneyZookeeperLock(userAmountDto);
    }

    /**
     * 用户提现
     * 数据库-分布式锁解决方案 - redisson可重入
     * @return
     * @paramp loginVo
     */
    @PostMapping("/user/takemoneyredisson")
    public R takeMoneyRedissonLock(@RequestBody UserAmountDto userAmountDto) {
        return userAmountRecordRedissonService.takeMoneyRedissonLock(userAmountDto);
    }

    /**
     * 用户提现
     * 数据库-分布式锁解决方案 - redisson不可重入
     * @return
     * @paramp loginVo
     */
    @PostMapping("/user/takemoneyredissontrylock")
    public R takeMoneyRedissonTryLock(@RequestBody UserAmountDto userAmountDto) {
        return userAmountRecordRedissonService.takeMoneyRedissonTryLock(userAmountDto);
    }
}
